package cn.stylefeng.guns.sys.core.listener;

import cn.stylefeng.guns.sys.modular.system.entity.BtyDysxf;
import cn.stylefeng.guns.sys.modular.system.service.BtyDysxfService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;


/*
 * 监听器批处理数据
 * */
public class DysxfExcelListener extends AnalysisEventListener<BtyDysxf> {

    private BtyDysxfService dysxfService;

    public DysxfExcelListener(){

    }

    public DysxfExcelListener(BtyDysxfService dysxfService){
        this.dysxfService = dysxfService;
    }


    /**
     * 批处理阈值2000
     */
    private static final int BATCH_COUNT = 100;
    List<BtyDysxf> list = new ArrayList<>();

    /**
     *
     * @param basic
     * @param analysisContext
     */
    @Override
    public void invoke(BtyDysxf basic, AnalysisContext analysisContext) {
        basic.setRegion(basic.getRegion().split("\\.")[0]);
        list.add(basic);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        analysisContext.readWorkbookHolder().setDefaultReturnMap(false);
        saveData();
        // log.info("所有数据解析完成！");

    }
    private void saveData(){
        //调用saveData()方法
        dysxfService.insertDysxf(list);
    }
}